/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package igraf.peritua;

import igraf.ItzuliInterfazea;
import igraf.Panela;
import igraf.diagnostikoak.EremuPrezioPane;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;

import dBasea.AbokatuFakt;
import dBasea.Balorazioak;
import dBasea.Inplikatuak;

public class BalorazioaEman extends Panela {
	private static final long serialVersionUID = -2102394899186353371L;

	private JLabel eremu;
	private JTable diru;
	private JButton ok;
	private JButton ezeztatu;
	private DefaultTableModel bkim;
	private JTextField textField;
	private JTextField textField_1;
	private AbokatuFakt abok = new AbokatuFakt();
	private JButton gehitu;
	private Balorazioak balo = new Balorazioak();
	private Inplikatuak inpli = new Inplikatuak();

	public BalorazioaEman(String tituloa,
			final ItzuliInterfazea itzuliInterfazea) {
		super(tituloa);

		JPanel erdikoPanela = this.getErdikoPanela();

		erdikoPanela.setBackground(SystemColor.activeCaption);
		erdikoPanela.setLayout(new BorderLayout());

		// Erdiko panelak
		JPanel erdikoa = new JPanel();
		erdikoa.setBackground(SystemColor.activeCaption);
		erdikoPanela.add(erdikoa, BorderLayout.CENTER);
		BorderLayout gl_erdikoa = new BorderLayout();
		erdikoa.setLayout(gl_erdikoa);

		JPanel arri = new JPanel();
		arri.setBackground(SystemColor.activeCaption);

		arri.setLayout(new BorderLayout());
		eremu = new JLabel("Eremu kaltetuta eta baloreak:");
		eremu.setHorizontalAlignment(SwingConstants.CENTER);
		eremu.setFont(new Font("Arial", Font.PLAIN, 14));
		arri.add(eremu, BorderLayout.NORTH);
		JPanel aux = new JPanel();
		aux.setBackground(SystemColor.activeCaption);
		aux.setLayout(new FlowLayout());

		arri.add(aux, BorderLayout.CENTER);

		bkim = new DefaultTableModel() {
			private static final long serialVersionUID = -3428680365628054985L;

			public boolean isCellEditable(int row, int col) {
				if (col == 0) {
					return false;
				}
				return true;
			}
		};
		bkim.addColumn("Eremu kaltetuta");
		bkim.addColumn("Behaketak");
		bkim.addColumn("Balorea");
		diru = new JTable(bkim);
		diru.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		JScrollPane scrollPane = new JScrollPane(diru,
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollPane.setPreferredSize(new Dimension(600, 350));
		aux.add(scrollPane);

		gehitu = new JButton("Gehitu eremua eta prezioa");
		aux.add(gehitu);

		erdikoa.add(arri);

		JPanel g = new JPanel();
		FlowLayout flowLayout_1 = (FlowLayout) g.getLayout();
		flowLayout_1.setVgap(15);
		flowLayout_1.setHgap(20);
		g.setBackground(SystemColor.activeCaption);

		JLabel lblNewLabel = new JLabel("Matrikula");
		g.add(lblNewLabel);

		textField = new JTextField();
		g.add(textField);
		textField.setColumns(10);
		textField.addKeyListener(new KeyAdapter() {// limitea NAN

					public void keyTyped(KeyEvent arg0) {
						if (textField.getText().length() >= 10) {
							arg0.consume();
						}
					}
				});

		JLabel lblNewLabel_1 = new JLabel("Istripua");
		g.add(lblNewLabel_1);
		try {
			textField_1 = new JFormattedTextField(
					new Integer(balo.lortuUrtea()));
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(null, "Ezin izan da istripua lortu",
					"Oker", JOptionPane.ERROR_MESSAGE);
			e1.printStackTrace();
		}

		g.add(textField_1);
		textField_1.setColumns(10);
		erdikoa.add(g, BorderLayout.SOUTH);

		// beheko panelak
		JPanel behekoa = new JPanel();
		FlowLayout flowLayout = (FlowLayout) behekoa.getLayout();
		flowLayout.setVgap(15);
		flowLayout.setHgap(110);
		behekoa.setBackground(SystemColor.activeCaption);
		ok = new JButton("Balorazioa sortu");

		ok.setHorizontalAlignment(SwingConstants.LEFT);
		ok.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ok);
		ezeztatu = new JButton("Ezeztatu");
		ezeztatu.setHorizontalAlignment(SwingConstants.RIGHT);
		ezeztatu.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ezeztatu);
		erdikoPanela.add(behekoa, BorderLayout.SOUTH);
		ezeztatu.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				itzuliInterfazea.itzuliLehenPanela();
			}
		});
		gehitu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new EremuPrezioPane(BalorazioaEman.this);
			}
		});
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					String matrikula = textField.getText();// matrikula
					int istripua = Integer.parseInt(textField_1.getText());// istripua

					if (!abok.badagoIstripua(istripua)) {
						JOptionPane.showMessageDialog(null,
								"Istripua ez da existitzen", "Oker",
								JOptionPane.ERROR_MESSAGE);
					} else if (!inpli.dagoMatrikula(matrikula)) {
						JOptionPane.showMessageDialog(null,
								"Matrikula ez da existitzen", "Oker",
								JOptionPane.ERROR_MESSAGE);
					} else {
						for (int i = 1; i <= bkim.getRowCount(); i++) {
							String eremua = (String) bkim.getValueAt(i - 1, 0);
							String behaketak = (String) bkim.getValueAt(i - 1,
									1);
							Float balorea = (Float) bkim.getValueAt(i - 1, 2);

							balo.balorazioaSortu(eremua, matrikula, istripua,
									behaketak, balorea);
						}

						JOptionPane.showMessageDialog(null,
								"Balorazioa sortu da", "Ondo",
								JOptionPane.INFORMATION_MESSAGE);
						eremuakGarbitu();
					}
				} catch (Exception e) {
					e.printStackTrace();
					JOptionPane.showMessageDialog(null,
							"Ez utzi zuriunerik mesedez! Bete eremu guztiak!",
							"Oker", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
	}

	public void addNewRow(String eremua, String behaketak, float balorea) {
		bkim.addRow(new Object[] { eremua, behaketak, balorea });
	}

	@Override
	public void eremuakGarbitu() {
		textField.setText("");
		textField_1.setText("");
		for (int i = bkim.getRowCount() - 1; i >= 0; i--) {
			bkim.removeRow(i);
		}
	}

}
